package com.ht.common.core.convert;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;
import com.ht.common.core.domain.vo.EntitySelectVo;

/**
 * 实体转下来列表转换器
 * @author Administrator
 */
public class EntitySelectConverter {

	public static <T> List<EntitySelectVo> convert(List<T> source, Function<T, String> codeGetter,
			Function<T, String> nameGetter) {
		return Optional.ofNullable(source).orElseGet(Collections::emptyList).stream().filter(Objects::nonNull)
				.map(item -> {
					String code = Optional.ofNullable(codeGetter.apply(item)).orElse("N/A");
					String name = Optional.ofNullable(nameGetter.apply(item)).orElse("Unknown");
					return new EntitySelectVo(code, name);
				}).collect(Collectors.toCollection(ArrayList::new));
	}
	
	public static <T> List<EntitySelectVo> convert(List<T> source, Function<T, String> codeGetter,
			Function<T, String> nameGetter,Function<T, String> ext1Getter) {
		return Optional.ofNullable(source).orElseGet(Collections::emptyList).stream().filter(Objects::nonNull)
				.map(item -> {
					String code = Optional.ofNullable(codeGetter.apply(item)).orElse("N/A");
					String name = Optional.ofNullable(nameGetter.apply(item)).orElse("Unknown");
					String extVal1 = Optional.ofNullable(ext1Getter.apply(item)).orElse("Unknown");
					return new EntitySelectVo(code, name,extVal1);
				}).collect(Collectors.toCollection(ArrayList::new));
	}
}
